package org.cskj.demo.service.imp;

import org.cskj.demo.mapper.UserMapper;
import org.cskj.demo.pojo.User;
import org.cskj.demo.pojo.UserExample;
import org.cskj.demo.util.UploadUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    private UserMapper userMapper;
    private String dir="/user";
    @Autowired
    private UploadUtil uploadUtil;
    public List<User> getall(int page, int pagesize){
        Map map=new HashMap();
        map.put("start",(page-1)*pagesize);
        map.put("end",pagesize);
        List<User> list = userMapper.selectLimit(map);
        return list;
    }
    public int count(){
        int i = userMapper.countByExample(null);
        return i;
    }
    public List<User> check(String name,String password){
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsnameEqualTo(name);
        criteria.andUspassEqualTo(password);
        criteria.andUstypeEqualTo(1);
        List<User> users = userMapper.selectByExample(example);
        return users;
    }
    public int insert(User user){
        int i = userMapper.insertSelective(user);
        return i;
    }
    public boolean checkname(String name){
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsnameEqualTo(name);

        List<User> users = userMapper.selectByExample(example);
        if (users.size()>0){
            return false;
        }else {
            return true;
        }
    }
    public boolean checkphone(String usphone){
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsphoneEqualTo(usphone);

        List<User> users = userMapper.selectByExample(example);
        if (users.size()>0){
            //不可以添加
            return false;
        }else {
            return true;
        }
    }
    public boolean checkcard(String icard){
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUscardEqualTo(icard);

        List<User> users = userMapper.selectByExample(example);
        if (users.size()>0){
            //不可以添加
            return false;
        }else {
            return true;
        }
    }
    public int update(User user){
        UserExample example = new UserExample();
        UserExample.Criteria criteria = example.createCriteria();
        criteria.andUsidEqualTo(user.getUsid());
        int i = userMapper.updateByExampleSelective(user,example);
        return i;
    }
    public User selectBykey(int id){
        User user = userMapper.selectByPrimaryKey(id);
        return user;
    }
    public String upload(MultipartFile file, HttpServletRequest request){
        String url = uploadUtil.upload(file, request, dir);
        return url;
    }
    public void deleteImg(String url, HttpServletRequest request){
        uploadUtil.delete(url,request,dir);
    }

}
